package com.ithe.canteen.service.impl;

import com.ithe.canteen.mapper.OrderMapper;
import com.ithe.canteen.pojo.Food;
import com.ithe.canteen.pojo.Order;
import com.ithe.canteen.pojo.OrderFood;
import com.ithe.canteen.service.FoodService;
import com.ithe.canteen.service.OrderService;
import com.ithe.canteen.util.CreatIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private FoodService foodService;

    @Value("${file.url}")
    private String filePath;

    @Override
    public List<Object> queryOrder(String customerID) {
        List<Order> orders = orderMapper.queryOrder(customerID);
        List<Object> orderList = new ArrayList<>();
        for (Order order : orders) {
            String foodID = order.getOrderFood();
            Food food = foodService.getFoodById(foodID);
            food.setFoodImage(filePath + food.getFoodImage());
            orderList.add(order);
            orderList.add(food);
        }
        return orderList;
    }

    @Override
    public List<OrderFood> managerQueryOrder(String dayQuery, String stateQuery) {
        List<Order> orders = orderMapper.managerQueryOrder(dayQuery, stateQuery);
        List<OrderFood> orderFoodDTOList = new ArrayList<>();

        for (Order order : orders) {
            String foodID = order.getOrderFood();
            Food food = foodService.getFoodById(foodID);

            // 设置食物图片的完整路径
            food.setFoodImage(filePath + food.getFoodImage());

            // 构建DTO对象
            OrderFood dto = new OrderFood(
                    order.getOrderId(),
                    order.getOrderUser(),
                    order.getOrderDate(),
                    order.getOrderState(),
                    food.getFoodID(),
                    food.getFoodImage(),
                    food.getFoodName(),
                    food.getFoodType(),
                    food.getFoodNum(),
                    food.getFoodPrice()
            );

            // 添加到列表
            orderFoodDTOList.add(dto);
        }

        return orderFoodDTOList;
    }

    @Override
    public Integer addOrder(String orderUser, String orderDate, String orderFood, String orderState) {
        String orderID = CreatIDUtil.createID("OR");
        return orderMapper.addOrder(orderID, orderUser, orderDate, orderFood, orderState);
    }

    @Override
    public Integer updateOrderState(String orderState, String orderID) {
        return orderMapper.updateOrderState(orderState, orderID);
    }

    @Override
    public Integer deleteOrder(String orderID) {
        return orderMapper.deleteOrder(orderID);
    }

    @Override
    public String getFoodID(String orderID) {
        return orderMapper.getFoodID(orderID);
    }
}
